package com.moon;

import io.rong.imkit.RongIM;
import io.rong.imkit.fragment.ConversationListFragment;
import io.rong.imkit.model.UIConversation;
import io.rong.imkit.widget.adapter.ConversationListAdapter;
import io.rong.imlib.model.Conversation;
import io.rong.imlib.model.Conversation.ConversationType;

import cn.yueliangbaba.R;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.util.PreferencesUtils;

import android.content.Context;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

public class InteractActivity extends FragmentActivity implements OnClickListener {
	public static String mClassName = "";
	String[] mTargetId;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_interact);
		
		enterFragment();
		
		findViewById(R.id.left).setVisibility(View.INVISIBLE);
		((TextView) findViewById(R.id.center)).setText("互动");
	}
	
	void setClass()
	{
		JSONArray classes = JSON.parseArray(PreferencesUtils.getString(this, "classes"));
		if(classes == null || classes.size() == 0) return;
		
		int size = classes.size();
		
		mTargetId = new String[size];
		for(int i = 0; i < size; i++)
		{
			JSONObject item = classes.getJSONObject(i);
			
			RelativeLayout layout = null;
			if(i == 0) layout = (RelativeLayout)findViewById(R.id.class0);
			if(i == 1) layout = (RelativeLayout)findViewById(R.id.class1);
			if(i == 2) layout = (RelativeLayout)findViewById(R.id.class2);
			if(i == 3) layout = (RelativeLayout)findViewById(R.id.class3);
			if(i == 4) layout = (RelativeLayout)findViewById(R.id.class4);
			layout.setOnClickListener(this);
			layout.setVisibility(View.VISIBLE);
			if(i == 0) findViewById(R.id.line0).setVisibility(View.VISIBLE);
			if(i == 1) findViewById(R.id.line1).setVisibility(View.VISIBLE);
			if(i == 2) findViewById(R.id.line2).setVisibility(View.VISIBLE);
			if(i == 3) findViewById(R.id.line3).setVisibility(View.VISIBLE);
			if(i == 4) findViewById(R.id.line4).setVisibility(View.VISIBLE);
			
			String className = item.getString("class_name");
			if(item.getString("class_alias") != null && item.getString("class_alias").length() > 0) className = item.getString("class_alias");

			if(i == 0) ((TextView)layout.findViewById(R.id.text0)).setText(className);
			if(i == 1) ((TextView)layout.findViewById(R.id.text1)).setText(className);
			if(i == 2) ((TextView)layout.findViewById(R.id.text2)).setText(className);
			if(i == 3) ((TextView)layout.findViewById(R.id.text3)).setText(className);
			if(i == 4) ((TextView)layout.findViewById(R.id.text4)).setText(className);
			
			layout.setTag(item);
			mTargetId[i] = item.getString("id");
		}
	}
	
	/**
	 * 加载 会话列表 ConversationListFragment
	 */
	private void enterFragment() {
		ConversationListFragment fragment = (ConversationListFragment) getSupportFragmentManager().findFragmentById(R.id.conversationlist);
//		mCustomAdapter = new CustomAdapter(RongContext.getInstance());
//		fragment.setAdapter(mCustomAdapter);
		Uri uri = Uri.parse("rong://" + getApplicationInfo().packageName).buildUpon()
				.appendPath("conversationlist")
				.appendQueryParameter(Conversation.ConversationType.PRIVATE.getName(), "false") //设置私聊会话非聚合显示
				.build();
		
		fragment.setUri(uri);
	}

	CustomAdapter mCustomAdapter = null;
	class CustomAdapter extends ConversationListAdapter 
	{
		public CustomAdapter(Context context) {
			super(context);
		}
		
		@Override
		protected View newView(Context context, int position, ViewGroup group) {
			return super.newView(context, position, group);
		}

		@Override
		protected void bindView(View v, int position, UIConversation data) {
			if (data.getConversationType().equals(Conversation.ConversationType.DISCUSSION)) 
				data.setUnreadType(UIConversation.UnreadRemindType.REMIND_ONLY);

			Object tag = v.getTag();
			super.bindView(v, position, data);
		}
	}
	
	protected void onResume()
	{
		super.onResume();
		setClass();
		setUnreadNum();
	}
	
	void setUnreadNum()
	{
		if(mTargetId != null)
		{
			findViewById(R.id.unread0).setVisibility(View.INVISIBLE);
			findViewById(R.id.unread1).setVisibility(View.INVISIBLE);
			findViewById(R.id.unread2).setVisibility(View.INVISIBLE);
			findViewById(R.id.unread3).setVisibility(View.INVISIBLE);
			findViewById(R.id.unread4).setVisibility(View.INVISIBLE);
			
			int count = mTargetId.length;
			for(int i = 0; i < count; i++)
			{
				int num = RongIM.getInstance().getRongIMClient().getUnreadCount(
						ConversationType.GROUP, mTargetId[i]);
				if(num > 0)
				{
					if(i == 0) 
					{
						findViewById(R.id.unread0).setVisibility(View.VISIBLE);
						((TextView)findViewById(R.id.unread0)).setText(String.valueOf(num));
					}
					if(i == 1) 
					{
						findViewById(R.id.unread1).setVisibility(View.VISIBLE);
						((TextView)findViewById(R.id.unread1)).setText(String.valueOf(num));
					}
					if(i == 2) 
					{
						findViewById(R.id.unread2).setVisibility(View.VISIBLE);
						((TextView)findViewById(R.id.unread2)).setText(String.valueOf(num));
					}
					if(i == 3) 
					{
						findViewById(R.id.unread3).setVisibility(View.VISIBLE);
						((TextView)findViewById(R.id.unread3)).setText(String.valueOf(num));
					}
					if(i == 4) 
					{
						findViewById(R.id.unread4).setVisibility(View.VISIBLE);
						((TextView)findViewById(R.id.unread4)).setText(String.valueOf(num));
					}
				}
			}
		}
		
		if(MainTabActivity.mMainTabActivity != null) MainTabActivity.mMainTabActivity.setChatMsg();
	}
	
	@Override
	public void onClick(View arg0) {
		JSONObject item = (JSONObject)arg0.getTag();
		
		int viewId = arg0.getId();
		switch(viewId)
		{
		case R.id.class0:
		case R.id.class1:
		case R.id.class2:
		case R.id.class3:
		case R.id.class4:
			mClassName = item.getString("class_name");
			RongIM.getInstance().startGroupChat(this, item.getString("id"), ((TextView)(arg0.findViewById(viewId + 2))).getText().toString());
			break;
		}
	}
	
	private long exitTime = 0;
	public void exitApp() {
		if ((System.currentTimeMillis() - exitTime) > 2000) 
		{
			Toast.makeText(this, "再按一次退出 应用", Toast.LENGTH_SHORT).show();
			exitTime = System.currentTimeMillis();
		} 
		else
		{
			finish();
		}
	}
	
	public boolean onKeyDown(int keyCode, KeyEvent event) {// 捕捉返回键
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			exitApp();
			return true;
		}
		return super.onKeyDown(keyCode, event);
	}
	
	@Override  
	public Resources getResources() {  
	    Resources res = super.getResources();    
	    Configuration config=new Configuration();    
	    config.setToDefaults();    
	    res.updateConfiguration(config,res.getDisplayMetrics() );  
	    return res;  
	}  
}
